/*
 * Copyright (c) 2017年11月06日  by XuanWu Wireless Technology Co., Ltd 
 *             All rights reserved  
 */
package com.zml.controller;

import com.zml.entity.Contact;
import com.zml.entity.ContactGroup;
import com.zml.service.ContactService;
import com.zml.utils.JsonResp;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author <a href="mailto:zhaominglin@wxchina.com">minglin.Zhao</a>
 * @Description
 * @Date 2017/11/6
 * @Version 1.0.0
 */
@Controller
@RequestMapping("/contact")
public class ContactController {

    private static final Logger logger = LogManager.getLogger(ContactController.class);

    @Autowired
    private ContactService contactService;

    @ResponseBody
    @RequestMapping(value = "all", method = RequestMethod.GET)
    public JsonResp getAll() {
        List<Contact> contacts = contactService.getAll();
        return JsonResp.success(contacts);
    }

    @ResponseBody
    @RequestMapping(value = "del/{id}", method = RequestMethod.DELETE)
    public JsonResp delete(@PathVariable("id") int id) {
        int result = contactService.deleteById(id);
        if (result == 1) {
            return JsonResp.success();
        } else {
            return JsonResp.fail("删除失败");
        }
    }

    @ResponseBody
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public JsonResp update(@RequestBody Contact contact){
        int result = contactService.updateContact(contact);
        if (result == 1) {
            return JsonResp.success();
        } else {
            return JsonResp.fail("更新失败");
        }
    }

    @ResponseBody
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public JsonResp add(@RequestBody Contact contact){
        int result = contactService.addContact(contact);
        if (result == 1) {
            return JsonResp.success();
        } else {
            return JsonResp.fail("更新失败");
        }
    }

    @ResponseBody
    @RequestMapping(value = "group",method = RequestMethod.GET)
    public JsonResp getGroups(){
        List<ContactGroup> contactGroups = contactService.getAllGroupNames();
        return JsonResp.success(contactGroups);
    }

    @ResponseBody
    @RequestMapping(value = "group/{name}",method = RequestMethod.DELETE)
    public JsonResp delGroup(@PathVariable("name") String name){
        int result = contactService.deleteGroupNameWithName(name);
        if (result == 1) {
            return JsonResp.success();
        } else {
            return JsonResp.fail("删除失败");
        }
    }
    @ResponseBody
    @RequestMapping(value = "group",method = RequestMethod.PUT)
    public JsonResp updateGroup(@RequestBody ContactGroup group){
        int result = contactService.updateGroupNameWithName(group);
        if (result == 1) {
            return JsonResp.success();
        } else {
            return JsonResp.fail("更新失败");
        }
    }

    @ResponseBody
    @RequestMapping(value = "group",method = RequestMethod.POST)
    public JsonResp createGroup(@RequestBody ContactGroup group){
        int result = contactService.createGroupNameWithName(group);
        System.err.println(group.getId());
        if (result == 1) {
            return JsonResp.success();
        } else {
            return JsonResp.fail("创建失败");
        }
    }

}
